package cn.lr.org.controller;

import cn.lr.basic.util.AjaxResult;
import cn.lr.basic.util.PageList;
import cn.lr.org.domain.Type;
import cn.lr.org.query.TypeQuery;
import cn.lr.org.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private ITypeService typeService;

    /**
     * 查询单个--restful风格
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Type queryById(@PathVariable("id") Long id){
        return typeService.loadById(id);
    }

    /**
     * 查询所有--restful风格
     * @return
     */
    @GetMapping
    public List<Type> queryAll(){
        return typeService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id) {
        try {
            typeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            typeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @PostMapping
    public PageList<Type> queryPage(@RequestBody TypeQuery query){
        return typeService.queryPage(query);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Type type) {
        try {
            if(type.getId() != null){
                typeService.update(type);
            } else
                typeService.save(type);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

}
